home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: alisa.org!wjjr
- From: wjjr@alisa.org (John J. Rushford)
- Subject: Re: Returning string from function (how?)
- X-Newsreader: TIN [version 1.2 PL2]
- Organization: My place on the Front Range.
- Message-ID: <Dp3AB5.FEu@alisa.org>
- References: <4jj4hh$m9n@news.xs4all.nl>
- Date: Sat, 30 Mar 1996 16:40:17 GMT
-
- Jordan (jordan@xs4all.nl) wrote:
- : Hi all, i'm trying to get a string returned from a function, but it's not
- : working as i planed.
- : Does anyone have some ideas?
-
-
- : something like this, but then working ;)
-
- : /*****************************************/
-
- : char s;
-
- : char get_str(void);
-
- : main()
- : {
- : s = get_str();
-
- : printf("%s", s);
- : }
-
- : char get_str(void)
- : {
- : int i;
- : char ch, string;
-
- : i=0;
- : printf("Enter a string: ");
- : while((ch = getchar()) != '\n' && i < MAX)
- : string[i++] = ch;
- : string[i] = '\0';
-
- : return string;
- : }
-
- I don't see where you are allocating space for 'string'. Also, when the
- function get_str() returns, the local variable 'string' is lost from the
- stack. How about:
-
- #define MAX 80
-
- char string[MAX]; /* global string with space allocated. */
-
- char *
- get_str ()
- {
- int i = 0;
- char ch;
-
- printf ("Enter a string: ");
- while ((ch = getchar ()) != '\n' && i < MAX)
- string[i++] = ch;
- string[i] = '\0';
-
- return string;
- }
-
- main ()
- {
- char * s;
-
- s = get_str ();
- printf ("%s", s);
- }
-
- regards
- --
- John J. Rushford
- Westminster, Colorado___/\_/\_
- wjjr@alisa.org (alisa.org is powered by FreeBSD)
-
-